<?php
namespace app\admin\controller;
use think\Input;
class Order extends Common{
    public function initialize(){
        parent::initialize();   
        $this->assign('shoplist',cache('shoplist'));   
        $this->assign('cionlist',cache('cionlist')); 
    }
    public function index(){
    	if(request()->isPost()){ 
            $where=[];
            $map='';
            $shopid = input('post.shopid');
            if($shopid) $where['shopid']=$shopid; 
            $timess = input('post.timess');            
            if($timess){ 
                $timess=explode(" - ",$timess);
                $starttime=strtotime($timess[0]);
                $endtime=strtotime($timess[1])+86399;    
                $map= [$starttime,$endtime];  
            }
            $timequan = input('post.timequan');
            if($timequan) $map= ''.$timequan.''; 
            $orderid = input('post.orderid');
            if($orderid) $where['orderid']=$orderid;  
            $orderid_out = input('post.orderid_out');
            if($orderid_out) $where['orderid_out']=$orderid_out;   
            $userid = input('post.userid');
            if($userid) $where['o.userid']=$userid;  
            $channelid = input('post.channelid');
            if($channelid) $where['o.channelid']=$channelid;  
            $agentid = input('post.agentid');
            if($agentid) $where['o.agentid']=$agentid;  

            $state = input('post.state');
            if($state)$where['o.state']=$state;
            $points = input('post.points');
            if($points)$where['points']=$points;

            $paytype = input('post.paytype');
            if($paytype)$where['ub.name_en']=$paytype;  
               
            $page =input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):config('pageSize');
            $list=db('channel_order')->alias('o')
                ->join(config('database.prefix') . 'user_banks ub', 'o.bankid = ub.id', 'left')
                ->join(config('database.prefix') . 'users u', 'o.userid = u.id', 'left')
                ->field('o.*,ub.name as payname,ub.bank_no,ub.bank_name,u.username')
                ->where($where)->whereTime('o.addtime',$map)->order('o.id desc')
                ->paginate(array('list_rows'=>$pageSize,'page'=>$page))
                ->toArray();   
            foreach ($list['data'] as $k=>$v){
                $list['data'][$k]['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
                if($v['paytime']) $list['data'][$k]['paytime'] = date('Y-m-d H:i',$v['paytime']);
                if($v['state']===0){ 
                    $list['data'][$k]['state_name'] ='<span class="red">待支付</span>';
                }else if($v['state']===1){
                    $list['data'][$k]['state_name'] ='<span class="blue">待确认</span>';
                }else if($v['state']===2){
                    $list['data'][$k]['state_name'] ='<span class="green">已确认</span>';
                }else if($v['state']===3){
                    $list['data'][$k]['state_name'] ='<span class="hui">已取消</span>';
                }else if($v['state']===-1){
                    $list['data'][$k]['state_name'] ='<span class="hui">已删除</span>';
                } 
                if($v['points']===0){
                    $list['data'][$k]['points_name'] ='<span class="orange">待上分</span>';
                }else if($v['points']===1){
                    $list['data'][$k]['points_name'] ='<span class="green">自动上分</span>';
                }else if($v['points']===2){
                    $list['data'][$k]['points_name'] ='<span class="green">手动上分</span>';
                }
                if($v['fei_user_jie']===0) $list['data'][$k]['fei_user']='<span class="orange" title="待结算">'.$v['fei_user'].'</span>';
                if($v['fei_shop_jie']===0) $list['data'][$k]['fei_shop']='<span class="orange" title="待结算">'.$v['fei_shop'].'</span>';
                if($v['fei_agent_jie']===0) $list['data'][$k]['fei_agent']='<span class="orange" title="待结算">'.$v['fei_agent'].'</span>';
            }
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        } 
        // 订单汇总
        // $wheres=[];
        // $maps='';
        // $_shopid = input('get.shopid');
        // if($_shopid){
        //     $wheres['shopid']=$_shopid; 
        //     $this->assign('_shopid',$_shopid);
        // }
        // $_timess = input('get.timess');            
        // if($_timess){ 
        //     $_timess=explode(" - ",$_timess);
        //     $starttime=strtotime($_timess[0]);
        //     $endtime=strtotime($_timess[1])+86399;    
        //     $maps= [$starttime,$endtime];  
        // }
        // $_timequan = input('get.timequan');
        // if($_timequan) $maps= ''.$_timequan.''; 
        // $_orderid = input('get.orderid');
        // if($_orderid) $wheres['orderid']=$_orderid;  
        // $_orderid_out = input('get.orderid_out');
        // if($_orderid_out) $wheres['orderid_out']=$_orderid_out;   
        // $_userid = input('get.userid');
        // if($_userid) $wheres['userid']=$_userid;  
        // $_channelid = input('get.channelid');
        // if($_channelid) $wheres['channelid']=$_channelid;  
        // $_agentid = input('get.agentid');
        // if($_agentid) $wheres['agentid']=$_agentid;  

        // $_state = input('get.state');
        // if($_state)$wheres['state']=$_state;
        // $_points = input('get.points');
        // if($_points)$wheres['points']=$_points;

        // $_paytype = input('get.paytype');
        // if($_paytype)$wheres['name_en']=$_paytype; 
        $payorder=[];
        $payorder['no']=db('channel_order')->where('state',0)->where($wheres)->sum('money');
        $payorder['que']=db('channel_order')->where('state',1)->where($wheres)->sum('money');
        $payorder['yes']=db('channel_order')->where('state',2)->where($wheres)->sum('money'); 
        $payorder['feishop']=db('channel_order')->where('state',2)->where($wheres)->sum('fei_shop'); 
        $payorder['feiagent']=db('channel_order')->where('state',2)->where($wheres)->sum('fei_agent'); 
        $payorder['feiuser']=db('channel_order')->where('state',2)->where($wheres)->sum('fei_user'); 
        $this->assign('payorder',$payorder);
        $this->assign('title','订单管理');
        return $this->fetch();
    } 
    // 小列表
    public function ordermin(){
        $this->assign('userid',input('userid'));
        $this->assign('channelid',input('channelid'));
        $this->assign('shopid',input('shopid')); 
        $this->assign('agentid',input('agentid')); 
        return $this->fetch();
    }
    // 成功订单
    public function oklist(){ 
         // 订单汇总
        $payorder=[]; 
        $payorder['yes']=db('channel_order')->where('state',2)->sum('money'); 
        $payorder['feishop']=db('channel_order')->where('state',2)->sum('fei_shop'); 
        $payorder['feiagent']=db('channel_order')->where('state',2)->sum('fei_agent'); 
        $payorder['feiuser']=db('channel_order')->where('state',2)->sum('fei_user'); 
        $this->assign('payorder',$payorder);
        return $this->fetch();
    }
    public function reback(){ 
        if(request()->isPost()){  
            $order=db('channel_order')->where(array('id'=>input('id'),'state'=>2,'points'=>0))->find();
            if($order){
                if(db('channel_order')->where('orderid',$order['orderid'])->update(['state'=>0,'paytime'=>''])!==false){
                    db('channel')->where('id',$order['channelid'])->where('shopappid',$order['shopappid'])->setDec('money_total',$order['money']);
                    return ['code'=>1,'msg'=>'订单变为未支付！'];
                }else{
                    return ['code'=>-1,'msg'=>'操作失败！']; 
                } 
            }else{
                return ['code'=>-1,'msg'=>'订单异常或不允许变更！'];
            }  
        }
    }
    // 删除订单
    public function dellist(){  
        return $this->fetch();
    }
    // 结算单 
    public function settlement(){  
        if(request()->isPost()){ 
            $page =input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):config('pageSize');
            $list=db('jiesuan')->alias('j')
                ->join(config('database.prefix') . 'users u', 'j.userid = u.id', 'left')
                ->join(config('database.prefix') . 'shop s', 'j.shopid = s.id', 'left')
                ->field('j.*,u.mobile,s.mobile as smobile,s.username as susername')
                ->order('j.addtime desc')
                ->paginate(array('list_rows'=>$pageSize,'page'=>$page))->toArray(); 
            foreach ($list['data'] as $k=>$v){
                $list['data'][$k]['addtime'] = date('Y-m-d H:i',$v['addtime']);
                if($v['type']=='user'){
                    $list['data'][$k]['typename']='码商'; 
                    $list['data'][$k]['shopname']=$v['username']; 
                }else if($v['type']=='agent'){
                    $list['data'][$k]['typename']='代理'; 
                    $list['data'][$k]['shopname']=$v['username']; 
                }else if($v['type']=='shop'){
                    $list['data'][$k]['typename']='商户'; 
                    $list['data'][$k]['username']=$v['susername']; 
                    $list['data'][$k]['mobile']=$v['smobile']; 
                }
                if($v['state']===0){
                    if($v['type']=='user'){
                        $list['data'][$k]['money_sf']=$v['money_a']-$v['money_s'];
                        $list['data'][$k]['money_sf']='<span class="green">+'.$list['data'][$k]['money_sf'].'</span>'; 
                    }else if($v['type']=='shop'){
                        $list['data'][$k]['money_sf']=$v['money_a']-$v['money_s'];
                        $list['data'][$k]['money_sf']='<span class="red">-'.$list['data'][$k]['money_sf'].'</span>'; 
                    }else if($v['type']=='agent'){
                        $list['data'][$k]['money_sf']='<span class="red">-'.$v['money_s'].'</span>'; 
                    }
                    $list['data'][$k]['state_name']='<span class="red">待处理</span>'; 
                }else{
                    if($v['type']=='user'){
                        $list['data'][$k]['money_sf']=$v['money_a']-$v['money_s'];
                        $list['data'][$k]['money_sf']='+'.$list['data'][$k]['money_sf']; 
                    }else if($v['type']=='shop'){
                        $list['data'][$k]['money_sf']=$v['money_a']-$v['money_s'];
                        $list['data'][$k]['money_sf']='-'.$list['data'][$k]['money_sf']; 
                    }else if($v['type']=='agent'){
                        $list['data'][$k]['money_sf']='-'.$v['money_s']; 
                    }
                    $list['data'][$k]['state_name']='<span class="green">已处理</span>'; 
                }
            }
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }
        // 今日未结算
        $money_today=db('channel_order')->where(array('state' =>2,'fei_user_jie' =>0,'fei_shop_jie' =>0))->whereTime('addtime','today')->sum('money');
        $order_today=db('channel_order')->where(array('state' =>2,'fei_user_jie' =>0,'fei_shop_jie' =>0))->whereTime('addtime','today')->count();
        $this->assign('money_today',$money_today);
        $this->assign('order_today',$order_today);
        // 昨日未结算
        $money_yesterday=db('channel_order')->where(array('state' =>2,'fei_user_jie' =>0,'fei_shop_jie' =>0))->whereTime('addtime','yesterday')->sum('money');
        $order_yesterday=db('channel_order')->where(array('state' =>2,'fei_user_jie' =>0,'fei_shop_jie' =>0))->whereTime('addtime','yesterday')->count();
        $this->assign('money_yesterday',$money_yesterday);
        $this->assign('order_yesterday',$order_yesterday);
        // 本周未结算
        $money_week=db('channel_order')->where(array('state' =>2,'fei_user_jie' =>0,'fei_shop_jie' =>0))->whereTime('addtime','week')->sum('money');
        $order_week=db('channel_order')->where(array('state' =>2,'fei_user_jie' =>0,'fei_shop_jie' =>0))->whereTime('addtime','week')->count();
        $this->assign('money_week',$money_week);
        $this->assign('order_week',$order_week);
        // 上周未结算
        $money_lastweek=db('channel_order')->where(array('state' =>2,'fei_user_jie' =>0,'fei_shop_jie' =>0))->whereTime('addtime','last week')->sum('money');
        $order_lastweek=db('channel_order')->where(array('state' =>2,'fei_user_jie' =>0,'fei_shop_jie' =>0))->whereTime('addtime','last week')->count();
        $this->assign('money_lastweek',$money_lastweek);
        $this->assign('order_lastweek',$order_lastweek);
        // 所有未结算        
        $money_all_no=db('channel_order')->where(array('state' =>2,'fei_user_jie' =>0,'fei_shop_jie' =>0))->sum('money');
        $order_all_no=db('channel_order')->where(array('state' =>2,'fei_user_jie' =>0,'fei_shop_jie' =>0))->count();
        $this->assign('money_all_no',$money_all_no);
        $this->assign('order_all_no',$order_all_no);
        // 所有已结算
        $money_all_yes=db('channel_order')->where(array('state' =>2,'fei_user_jie' =>1,'fei_shop_jie' =>1))->sum('money');
        $order_all_yes=db('channel_order')->where(array('state' =>2,'fei_user_jie' =>1,'fei_shop_jie' =>1))->count();
        $this->assign('money_all_yes',$money_all_yes);
        $this->assign('order_all_yes',$order_all_yes);  
        // 结算汇总
        $money_get_no=db('jiesuan')->where(array('type' =>'user','state' =>0))->field('SUM(money_a) as moneya,SUM(money_s) as moneys')->select(); 
        $this->assign('money_get_no',$money_get_no[0]['moneya']-$money_get_no[0]['moneys']);

        $money_get_yes=db('jiesuan')->where(array('type' =>'user','state' =>1))->field('SUM(money_a) as moneya,SUM(money_s) as moneys')->select(); 
        $this->assign('money_get_yes',$money_get_yes[0]['moneya']-$money_get_yes[0]['moneys']);

        $money_pay_no_shop=db('jiesuan')->where(array('type' =>'shop','state' =>0))->field('SUM(money_a) as moneya,SUM(money_s) as moneys')->select();         
        $money_pay_no_agent=db('jiesuan')->where(array('type' =>'agent','state' =>0))->sum('money_s');
        $this->assign('money_pay_no',$money_pay_no_shop[0]['moneya']-$money_pay_no_shop[0]['moneys']+$money_pay_no_agent);


        $money_pay_yes_shop=db('jiesuan')->where(array('type' =>'shop','state' =>1))->field('SUM(money_a) as moneya,SUM(money_s) as moneys')->select();         
        $money_pay_yes_agent=db('jiesuan')->where(array('type' =>'agent','state' =>1))->sum('money_s');
        $this->assign('money_pay_yes',$money_pay_yes_shop[0]['moneya']-$money_pay_yes_shop[0]['moneys']+$money_pay_yes_agent);


        return $this->fetch();
    } 
    // 小结算单 
    public function settlementmin(){  
        if(request()->isPost()){  
            $shopid = input('post.shopid')?input('post.shopid'):0;
            if($shopid) $where['shopid']=$shopid; 
            $userid = input('post.userid')?input('post.userid'):0;
            if($userid) $where['userid']=$userid;  
            $page =input('page')?input('page'):1;

            $pageSize =input('limit')?input('limit'):config('pageSize');
            $list=db('jiesuan')->alias('j')
                ->join(config('database.prefix') . 'users u', 'j.userid = u.id', 'left')
                ->join(config('database.prefix') . 'shop s', 'j.shopid = s.id', 'left')->where($where)
                ->field('j.*,u.mobile,s.mobile as smobile,s.username as susername')
                ->order('j.addtime desc')
                ->paginate(array('list_rows'=>$pageSize,'page'=>$page))->toArray(); 
            foreach ($list['data'] as $k=>$v){
                $list['data'][$k]['addtime'] = date('Y-m-d H:i',$v['addtime']);
                if($v['type']=='user'){
                    $list['data'][$k]['typename']='码商'; 
                    $list['data'][$k]['shopname']=$v['username']; 
                }else if($v['type']=='agent'){
                    $list['data'][$k]['typename']='代理'; 
                    $list['data'][$k]['shopname']=$v['username']; 
                }else if($v['type']=='shop'){
                    $list['data'][$k]['typename']='商户'; 
                    $list['data'][$k]['username']=$v['susername']; 
                    $list['data'][$k]['mobile']=$v['smobile']; 
                }
                if($v['state']===0){
                    if($v['type']=='user'){
                        $list['data'][$k]['money_sf']=$v['money_a']-$v['money_s'];
                        $list['data'][$k]['money_sf']='<span class="green">+'.$list['data'][$k]['money_sf'].'</span>'; 
                    }else if($v['type']=='shop'){
                        $list['data'][$k]['money_sf']=$v['money_a']-$v['money_s'];
                        $list['data'][$k]['money_sf']='<span class="red">-'.$list['data'][$k]['money_sf'].'</span>'; 
                    }else if($v['type']=='agent'){
                        $list['data'][$k]['money_sf']='<span class="red">-'.$v['money_s'].'</span>'; 
                    }
                    $list['data'][$k]['state_name']='<span class="red">待处理</span>'; 
                }else{ 
                    if($v['type']=='user'){
                        $list['data'][$k]['money_sf']=$v['money_a']-$v['money_s'];
                        $list['data'][$k]['money_sf']='+'.$list['data'][$k]['money_sf']; 
                    }else if($v['type']=='shop'){
                        $list['data'][$k]['money_sf']=$v['money_a']-$v['money_s'];
                        $list['data'][$k]['money_sf']='-'.$list['data'][$k]['money_sf']; 
                    }else if($v['type']=='agent'){
                        $list['data'][$k]['money_sf']='-'.$v['money_s']; 
                    }
                    $list['data'][$k]['state_name']='<span class="green">已处理</span>'; 
                }
            }
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        } 
        $this->assign('shopid',input('shopid'));
        $this->assign('userid',input('userid')); 
        return $this->fetch();
    } 
    
    public function pass_jiesuan(){
        if(request()->isPost()){  
            if(db('jiesuan')->where(array('id'=>input('id'),'state'=>0))->update(['state'=>1])!==false){
                return ['code'=>1,'msg'=>'操作成功！'];
            }else{
                return ['code'=>-1,'msg'=>'操作失败！'];
            } 
        }
    }
    // 结算操作——码商
    public function jieshu(){  
        if(request()->isPost()){ 
            $map='';
            $timequ = input('post.timequ');            
            if($timequ) $map= ''.$timequ.''; 
            $list_user=db('channel_order')->where(array('state' =>2,'fei_user_jie' =>0))->whereTime('addtime',$map)
                ->group('userid')->order('id desc')->select();
            if($list_user){
                foreach ($list_user as $k=>$v){ 
                    $order_user=db('channel_order')->where(array('state' =>2,'fei_user_jie' =>0,'userid'=>$v['userid']))->whereTime('addtime',$map)->select(); 
                    $money_s[$k]=0;
                    $money_a[$k]=0;
                    $number[$k]=0;
                    foreach ($order_user as $kk=>$vv){
                        $money_s[$k] +=$vv['fei_user'];
                        $money_a[$k] +=$vv['money'];
                        $number[$k] +=1;
                        db('channel_order')->where('id',$vv['id'])->update(['fei_user_jie'=>1,'fei_user_time'=>time()]);
                    } 
                    $username[$k]=db('users')->where('id',$v['userid'])->value('username');
                    $datas[$k]= array(
                        'orderid' =>'J'.rand_string(8,1).'S'.$v['userid'], 
                        'money_s' =>$money_s[$k], 
                        'money_a' =>$money_a[$k], 
                        'number' =>$number[$k], 
                        'state' =>0, 
                        'type' =>'user', 
                        'userid' =>$v['userid'], 
                        'username' =>$username[$k], 
                        'addtime' =>time(), 
                    ); 
                    db('jiesuan')->insertGetId($datas[$k]);
                }
                return $result = ['code'=>1,'msg'=>'码商结算完成，即将结算商户！'];
            }else{
                return $result = ['code'=>0,'msg'=>'无需结算!'];
            }  
        } 
    }
    // 结算商户
    public function jieshu_shop(){  
        if(request()->isPost()){ 
            $map='';
            $timequ = input('post.timequ');            
            if($timequ) $map= ''.$timequ.''; 
            $list_shop=db('channel_order')->where(array('state' =>2,'fei_shop_jie' =>0))->whereTime('addtime',$map)
                ->group('shopid')->order('id desc')->select();
            if($list_shop){
                foreach ($list_shop as $k=>$v){ 
                    $order_shop=db('channel_order')->where(array('state' =>2,'fei_shop_jie' =>0,'shopid'=>$v['shopid']))->whereTime('addtime',$map)->select();
                    $money_s[$k]=0;
                    $money_a[$k]=0;
                    $number[$k]=0;
                    foreach ($order_shop as $kk=>$vv){
                        $money_s[$k] +=$vv['fei_shop'];
                        $money_a[$k] +=$vv['money'];
                        $number[$k] +=1;
                        db('channel_order')->where('id',$vv['id'])->update(['fei_shop_jie'=>1,'fei_shop_time'=>time()]); 
                    } 
                    $datas[$k]= array(
                        'orderid' =>'J'.rand_string(8,1).'S'.$v['shopid'], 
                        'money_s' =>$money_s[$k], 
                        'money_a' =>$money_a[$k], 
                        'number' =>$number[$k], 
                        'state' =>0, 
                        'type' =>'shop', 
                        'shopid' =>$v['shopid'], 
                        'shopname' =>$v['shopname'], 
                        'addtime' =>time(), 
                    ); 
                    db('jiesuan')->insertGetId($datas[$k]);
                }
                return $result = ['code'=>1,'msg'=>'商户结算完成，即将结算代理!'];
            }else{
                return $result = ['code'=>0,'msg'=>'无需结算!'];
            }  
        } 
    }
    //结算代理
    public function jieshu_agent(){  
        if(request()->isPost()){ 
            $map='';
            $timequ = input('post.timequ');            
            if($timequ) $map= ''.$timequ.''; 
            $list_user=db('channel_order')->where(array('state' =>2,'fei_agent_jie' =>0))->whereTime('addtime',$map)
                ->group('agentid')->order('id desc')->select();
            if($list_user){
                foreach ($list_user as $k=>$v){ 
                    $order_user=db('channel_order')->where(array('state' =>2,'fei_agent_jie' =>0,'agentid'=>$v['agentid']))->whereTime('addtime',$map)->select(); 
                    $money_s[$k]=0;
                    $money_a[$k]=0;
                    $number[$k]=0;
                    foreach ($order_user as $kk=>$vv){
                        $money_s[$k] +=$vv['fei_agent'];
                        $money_a[$k] +=$vv['money'];
                        $number[$k] +=1;
                        db('channel_order')->where('id',$vv['id'])->update(['fei_agent_jie'=>1,'fei_agent_time'=>time()]);
                    } 
                    if($v['agentid']){
                        $username[$k]=db('users')->where('id',$v['agentid'])->value('username');
                        $datas[$k]= array(
                            'orderid' =>'J'.rand_string(8,1).'S'.$v['agentid'], 
                            'money_s' =>$money_s[$k], 
                            'money_a' =>$money_a[$k], 
                            'number' =>$number[$k], 
                            'state' =>0, 
                            'type' =>'agent', 
                            'userid' =>$v['agentid'], 
                            'username' =>$username[$k], 
                            'addtime' =>time(), 
                        ); 
                        db('jiesuan')->insertGetId($datas[$k]);
                    } 
                }
                return $result = ['code'=>1,'msg'=>'结算结束'];
            }else{
                return $result = ['code'=>0,'msg'=>'无需结算!'];
            }  
        } 
    }

     //批量清除订单
    public function delorderlist()
    {
        if(request()->isPost()){ 
            $where=[];
            $map='';
            $shopid = input('post.shopid');
            if($shopid) $where['shopid']=$shopid; 
            $timess = input('post.timess');            
            if($timess){ 
                $timess=explode(" - ",$timess);
                $starttime=strtotime($timess[0]);
                $endtime=strtotime($timess[1])+86399;    
                $map= [$starttime,$endtime];  
            }
            $timequan = input('post.timequan');
            if($timequan) $map= ''.$timequan.''; 
            $orderid = input('post.orderid');
            if($orderid) $where['orderid']=$orderid;  
            $orderid_out = input('post.orderid_out');
            if($orderid_out) $where['orderid_out']=$orderid_out;   
            $userid = input('post.userid');
            if($userid) $where['o.userid']=$userid;  
            $channelid = input('post.channelid');
            if($channelid) $where['o.channelid']=$channelid;  
            $agentid = input('post.agentid');
            if($agentid) $where['o.agentid']=$agentid;  

            $state = input('post.state');
            if($state)$where['o.state']=$state;

            $page =input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):config('pageSize');
            $list=db('channel_order')->alias('o')
                ->join(config('database.prefix') . 'user_banks ub', 'o.bankid = ub.id', 'left')
                ->join(config('database.prefix') . 'users u', 'o.userid = u.id', 'left')
                ->field('o.*,ub.name as payname,ub.bank_no,ub.bank_name,u.username')
                ->where($where)->whereTime('o.addtime',$map)->order('o.id desc')
                ->paginate(array('list_rows'=>$pageSize,'page'=>$page))
                ->toArray();   
            foreach ($list['data'] as $k=>$v){
                $list['data'][$k]['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
                if($v['paytime']) $list['data'][$k]['paytime'] = date('Y-m-d H:i',$v['paytime']);
                if($v['state']===0){ 
                    $list['data'][$k]['state_name'] ='<span class="red">待支付</span>';
                }else if($v['state']===1){
                    $list['data'][$k]['state_name'] ='<span class="blue">待确认</span>';
                }else if($v['state']===2){
                    $list['data'][$k]['state_name'] ='<span class="green">已确认</span>';
                }else if($v['state']===3){
                    $list['data'][$k]['state_name'] ='<span class="hui">已取消</span>';
                }
            }
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }elseif (request()->isGet()) {
            $map = [];
            if(!empty(input('get.shopid'))){
                $shopid = input('get.shopid');
                $map['shopid'] = ['shopid','=',$shopid];
            }

            if(!empty(input('get.timess'))){
                $timess = input('get.timess');
                $time= explode(" - ", $timess);
                $s_time = strtotime($time[0]);
                $e_time = strtotime($time[1])+86399;
                $map = ' addtime>= '.$s_time;
                $map .= ' and addtime<= '.$e_time;                

            }
            if(!empty(input('get.orderid'))){
                $orderid = input('get.orderid');
                $map['orderid'] = ['orderid','=',$orderid];
            }
            if(!empty(input('get.orderid_out'))){
                $orderid_out = input('get.orderid_out');
                $map['orderid_out'] = ['orderid_out','=',$orderid_out];
            }
            
            if(!empty($map)){
                if(db('channel_order')->where($map)->delete() !== false){
                    $map='';
                    echo "<script>alert('删除成功！');;</script>";
                    $this->success('删除成功！','delorderlist');
                    return;
                }elseif(db('channel_order')->where($map)->delete() === false){
                    $map='';
                    echo "<script>alert('删除失败，确定该订单存在！');</script>";
                    $this->error('删除失败！','delorderlist');
                    return;
                }
            }

        }

        return $this->fetch();
    }
}